package org.droidexpress.tools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.droidexpress.R;
import org.droidexpress.data.Paquet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;

public class PaquetAdapter extends BaseAdapter implements Serializable, OnCheckedChangeListener, OnItemSelectedListener {

	private static final long serialVersionUID = 1L;
	List<Paquet> listePaquet;
	LayoutInflater inflater;
	Context mContext;
	ViewHolder holder;
	
	public PaquetAdapter(Context context, List<Paquet> nListeDest)
	{
		inflater = LayoutInflater.from(context);
		listePaquet = nListeDest;
		SingletonPaquet.getInstance().refus = new ArrayList<Boolean>();
		SingletonPaquet.getInstance().motif = new ArrayList<Integer>();
		for(Paquet p : nListeDest)
		{
			SingletonPaquet.getInstance().refus.add(false);
			SingletonPaquet.getInstance().motif.add(0);
		}
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return listePaquet.size();
	}
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listePaquet.get(position);
	}
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(convertView == null)
		{
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.listpaquets, null);
			holder.cbAccepter = (CheckBox) convertView.findViewById(R.id.listPaquetsNom);
			holder.spMotif = (Spinner) convertView.findViewById(R.id.listPaquetsMotif);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		boolean accepter = false;
				
		holder.cbAccepter.setChecked(accepter);
		holder.cbAccepter.setText(listePaquet.get(position).code_barre);
		holder.cbAccepter.setOnCheckedChangeListener(this);
		
		holder.spMotif.setOnItemSelectedListener(this);
		
		return convertView;
	}
	
	public class ViewHolder{
		CheckBox cbAccepter;
		Spinner spMotif;
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch(buttonView.getId())
		{
		case R.id.listPaquetsNom:
			SingletonPaquet sP = SingletonPaquet.getInstance();
			int i = 0;
			
			for(Paquet p : listePaquet)
			{
				if(p.code_barre == ((CheckBox) buttonView.findViewById(R.id.listPaquetsNom)).getText().toString())
				{
					sP.position = i;
					sP.refus.set(i, ((CheckBox) buttonView.findViewById(R.id.listPaquetsNom)).isChecked());
					
				}
				i++;
			}
		}
	}

	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		//arg0 parent , arg 1 vue , arg2 position element, arg3 id
		// TODO Auto-generated method stub
		/*switch(arg0.getId())
		{
		case R.id.listPaquetsMotif:
			SingletonPaquet sP = SingletonPaquet.getInstance();
			int i = 0;
			for(Paquet p : listePaquet)
			{
				for(int j = 0; j < 3; j++)
				{
					if(sP.motif.get(i) != j)
					{
						sP.position = i;
						sP.motif.set(i, arg2);
					}
				}
				i++;
			}
			
			break;
		}*/
		
		//Object item = arg0.getItemAtPosition(arg2);
		//System.out.println(item.toString());
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
}
